<?php

/**
 * Description of DomaineApplication
 *
 * @author Jeremy
 * @version 1.0
 */
class DomaineApplication
{

    private $IdDomaineApplication;
    private $NomDomaineApplication;
    public $requeteOk;
    public $errormsg;
 
    /**
     *
     * @param int $idD
     */
    function __construct($idD = 0)
    {
        if (isset($idD) && !empty($idD))
        {
            $sql = SQL::getInstance()->getConnection();

            $query = "SELECT DomaineApplication.* FROM DomaineApplication WHERE idDomaineApplication = :id";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $idD, PDO::PARAM_INT);

            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);

                $this->IdDomaineApplication = $result->idDomaineApplication;
                $this->NomDomaineApplication = $result->NomDomaineApplication;

                $this->errormsg = "Domaine application recuperee";
                $this->requeteOk = true;
            }
            else
            {
                $this->IdDomaineApplication = 0;
                $this->NomDomaineApplication = "";

                $this->errormsg = "SQL Failed";
                $this->requeteOk = false;
            }
            $c->closeCursor();
        }
        else
        {
            $this->IdDomaineApplication = 0;
            $this->NomDomaineApplication = "";

            $this->errormsg = "Domaine application vierge";
        }
    }

    /**
     *
     * @return bool True si la suppression s'est correctment effectuee
     */
    function delete()
    {
        if (!empty($this->IdDomaineApplication))
        {
            $sql = SQL::getInstance()->getConnection();

            $query = "DELETE FROM DomaineApplication WHERE idDomaineApplication :id";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $this->IdDomaineApplication, PDO::PARAM_INT);

            if ($c->execute())
            {
                return true;
            }
            else
            {
                $this->errormsg = $c->errorInfo();
                $this->requeteOk = false;
            }

            $c->closeCursor();
        }

        return false;
    }

    /**
     * Sauvegarde le domaine dans la base
     * @return int Id du domaine
     */
    public function save()
    {
        if (!empty($this->NomDomaineApplication))
        {
            $sql = SQL::getInstance()->getConnection();

            if (!empty($this->IdDomaineApplication))
            {
                // DomaineApplication definit on "update"
                $query = "UPDATE DomaineApplication
                         SET
                            NomDomaineApplication = :nom
                         WHERE idDomaineApplication = :id";
                $sql->beginTransaction();
                try
                {
                    $c = $sql->prepare($query);
                    $c->bindParam(":id", $this->IdDomaineApplication, PDO::PARAM_INT);

                    $c->bindParam(":nom", $this->NomDomaineApplication, PDO::PARAM_STR, 255);

                    if ($c->execute())
                    {
                        $sql->commit();
                        $this->requeteOk = true;
                    }
                    else
                    {
                        $sql->rollBack();
                        $this->requeteOk = false;
                        $this->errormsg = $c->errorInfo();
                    }

                    $c->closeCursor();
                }
                catch (PDOException $ex)
                {
                    $this->errormsg = $ex->getMessage();
                    $this->requeteOk = false;
                }
            }
            else
            {
                // Nouveau DomaineApplication on peut "insert"
                $query = "INSERT INTO DomaineApplication
                            (NomDomaineApplication)
                          VALUES(:nom)";

                $sql->beginTransaction();
                try
                {
                    $c = $sql->prepare($query);

                    $c->bindParam(":nom", $this->NomDomaineApplication, PDO::PARAM_STR, 255);

                    if ($c->execute())
                    {
                        $this->IdDomaineApplication = $sql->lastInsertId();
                        $sql->commit();
                        $this->requeteOk = true;
                    }
                    else
                    {
                        $sql->rollBack();
                        $this->requeteOk = false;
                        $this->errormsg = $c->errorInfo();
                    }

                    $c->closeCursor();
                }
                catch (PDOException $ex)
                {
                    $this->errormsg = $ex->getMessage();
                    $this->requeteOk = false;
                }
            }
            return $this->IdDomaineApplication;
        }
        else
            return 0;
    }

    /**
     * Getter / Setter
     */

    /**
     *
     * @return int
     */
    public function getIdDomaineApplication()
    {
        return $this->IdDomaineApplication;
    }

    /**
     *
     * @return string
     */
    public function getNomDomaineApplication()
    {
        return $this->NomDomaineApplication;
    }

    /**
     *
     * @param string $NomDomaineApplication
     * @return mixed
     */
    public function setNomDomaineApplication($NomDomaineApplication)
    {
        $err = array("bool" => true, "msg" => "");
        if (isset($NomDomaineApplication) && !empty($NomDomaineApplication))
        {
            if (strlen($NomDomaineApplication) < 255)
            {
                $this->NomDomaineApplication = htmlentities($NomDomaineApplication);
            }
            else
            {
                $err["bool"] = false;
                $err["msg"] = "Nom trop long";
            }
        }
        else
        {
            $err["bool"] = false;
            $err["msg"] = "Nom vide";
        }
        return $err;
    }

    /**
     *
     * @return mixed Array de Domaine
     * @static
     */
    static public function getListDomaineApplication()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT * FROM DomaineApplication";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);

        if (!empty($result))
        {
            return $result;
        }
        return array();
    }

    /**
     *
     * @param int $limit
     * @param int $begin
     * @static
     * @return mixed Array de Domaine
     */
    static public function getList($limit=0, $begin=0)
    {
        if (empty($limit))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = 'SELECT * FROM DomaineApplication';
            $c = $sql->query($query);
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            $query = 'SELECT *
                              FROM DomaineApplication';

            $query .= ' LIMIT :begin,:limit';
            $c = $sql->prepare($query);
            $c->bindParam(':begin', $begin, PDO::PARAM_INT);
            $c->bindParam(':limit', $limit, PDO::PARAM_INT);
            if ($c->execute())
            {
                $result = $c->fetchAll(PDO::FETCH_OBJ);
                return $result;
            }
            else
            {
                return array();
            }
        }
    }

    /**
     *
     * @param string $name
     * @param int $promo
     * @return mixed Array de domain
     */
    static public function globalCount($name="", $promo =0)
    {
        $sql = SQL::getInstance()->getConnection();
        $query = 'SELECT COUNT(IdDomaineApplication) as count
				  FROM DomaineApplication';

        $c = $sql->prepare($query);
        if ($c->execute())
        {
            $result = $c->fetch(PDO::FETCH_OBJ);
            return $result->count;
        }
        else
        {
            return array();
        }
    }

}

?>